package middleware

import (
	"fmt"
	jwt_enum "oc_saas/enum/jwt"
	"oc_saas/utils/tool"

	"github.com/gin-gonic/gin"
)

// JWT验证
func JWTAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.Request.Header.Get("x-token")
		if token == "" {
			c.Abort()
			panic(jwt_enum.UNAUTHORIZED_ACCESS)
		}
		j := new(tool.JWTAuth)
		if claims, err := j.ParseToken(token); err != nil {
			c.Abort()
			panic(err)
		} else {
			c.Set("claims", claims)
			fmt.Println(claims)
			c.Next()
		}
	}
}
